# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

#!/bin/bash

# Fail on errors
set -e

# Set env vars.
UID_ARG=$1
GID_ARG=$2
MINIFI_VERSION=$3
MINIFI_SOURCE_CODE=$4
CMAKE_SOURCE_DIR=$5
DUMP_LOCATION=$6
DISTRO_NAME=$7
ENABLE_JNI=$8

echo "NiFi-MiNiFi-CPP Version: $MINIFI_VERSION"
echo "Current Working Directory: $(pwd)"
echo "CMake Source Directory: $CMAKE_SOURCE_DIR"
echo "MiNiFi Package: $MINIFI_SOURCE_CODE"

# Copy the MiNiFi source tree to the Docker working directory before building
rm -rf $CMAKE_SOURCE_DIR/docker/${DISTRO_NAME}/minificppsource/
mkdir -p $CMAKE_SOURCE_DIR/docker/${DISTRO_NAME}/minificppsource
rsync -avr \
      --exclude '/*build*' \
      --exclude '/docker' \
      --exclude '.git' \
      --exclude '/extensions/expression-language/Parser.cpp' \
      --exclude '/extensions/expression-language/Parser.hpp' \
      --exclude '/extensions/expression-language/Scanner.cpp' \
      --exclude '/extensions/expression-language/location.hh' \
      --exclude '/extensions/expression-language/position.hh' \
      --exclude '/extensions/expression-language/stack.hh' \
      --delete \
      $CMAKE_SOURCE_DIR/ \
      $CMAKE_SOURCE_DIR/docker/${DISTRO_NAME}/minificppsource/

DOCKER_COMMAND="docker build --build-arg UID=$UID_ARG \
                             --build-arg GID=$GID_ARG \
                             --build-arg MINIFI_VERSION=$MINIFI_VERSION \
                             --build-arg MINIFI_SOURCE_CODE=$MINIFI_SOURCE_CODE \
                             --build-arg DUMP_LOCATION=$DUMP_LOCATION \
                             --build-arg ENABLE_JNI=$ENABLE_JNI \
                             -t \
                             apacheminificpp:${DISTRO_NAME}-$MINIFI_VERSION ."
echo "Docker Command: '$DOCKER_COMMAND'"
${DOCKER_COMMAND}

docker run --rm --entrypoint cat apacheminificpp:${DISTRO_NAME}-$MINIFI_VERSION /opt/minifi/build/nifi-minifi-cpp-$MINIFI_VERSION-bin.tar.gz > ${DUMP_LOCATION}/nifi-minifi-cpp-${DISTRO_NAME}-$MINIFI_VERSION-bin.tar.gz

rm -rf $CMAKE_SOURCE_DIR/docker/${DISTRO_NAME}/minificppsource/
